<template>
  <div class="admin-layout">
    <!-- 移动端遮罩层 -->
    <div v-if="isMobile && !isCollapsed" class="sidebar-overlay" @click="toggleSidebar"></div>

    <!-- 侧边栏 -->
    <Sidebar :is-collapsed="isCollapsed" :is-mobile="isMobile" @toggle-sidebar="toggleSidebar" />

    <!-- 主内容区 -->
    <div class="main-container" :class="{ collapsed: isCollapsed, 'is-mobile': isMobile }">
      <!-- 顶部导航栏 -->
      <Header />

      <!-- 内容区域 -->
      <div class="content">
        <slot></slot>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue';
import Sidebar from './Sidebar.vue';
import Header from './Header.vue';

const isCollapsed = ref(false);
const isMobile = ref(false);

const checkMobile = () => {
  isMobile.value = window.innerWidth <= 768;
  // 在移动端默认折叠侧边栏
  if (isMobile.value) {
    isCollapsed.value = true;
  }
};

const toggleSidebar = () => {
  isCollapsed.value = !isCollapsed.value;
};

onMounted(() => {
  checkMobile();
  window.addEventListener('resize', checkMobile);
});

onUnmounted(() => {
  window.removeEventListener('resize', checkMobile);
});
</script>

<style scoped>
.admin-layout {
  display: flex;
  height: 100vh;
  background-color: #f0f2f5;
  position: relative;
}

/* 移动端遮罩层 */
.sidebar-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  z-index: 99;
}

/* 主容器样式 */
.main-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  transition: all 0.3s;
  width: calc(100% - 220px);
}

.main-container.collapsed {
  width: calc(100% - 64px);
}

/* 内容区域样式 */
.content {
  flex: 1;
  padding: 15px;
  overflow-y: auto;
  min-height: 0;
  /* 防止flex布局下内容溢出 */
}

/* 响应式设计 */
@media (max-width: 768px) {
  .main-container {
    width: 100%;
  }

  .main-container.collapsed {
    width: 100%;
  }
}

@media (max-width: 480px) {
  .content {
    padding: 10px;
  }
}
</style>